package cn.q3c.common.util;

import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 对象工具集，如list排序，过滤等操作，需要用到反射
 * @author wangxinning
 *
 */
public class ObjectUtils {

	/**
	 * 返回-1表示转换失败，也就是说参数!=-1
	 * @param l
	 * @return
	 */
	public static int longToInt(long l) {
		if( l <= Integer.MAX_VALUE && l > Integer.MIN_VALUE )
		{
			return NumberUtils.toInt(Long.toString(l));
		}
		else
		{
			return -1;
		}
	}
	
	public static String formatDouble( double d, String format )
	{
		DecimalFormat df = new DecimalFormat(format);//("###0.0#");//最多保留几位小数，就用几个#，最少位就用0来确定  
		return df.format(d);  
	}
	
	/**
	 * 返回当前距离参数的时间
	 * @param time
	 * @return
	 */
	public static long getTimeDiffWithNow( long time, int type )
	{
		long danwei = 1;
		if( type == 1 )//ms
		{
			
		}
		else if( type == 2 )//s
		{
			danwei = DateUtils.MILLIS_PER_SECOND;
		}
		else if( type == 3 )//m
		{
			danwei = DateUtils.MILLIS_PER_MINUTE;
		}
		else if( type == 4 )//h
		{
			danwei = DateUtils.MILLIS_PER_HOUR;
		}
		else if( type == 5 )//d
		{
			danwei = DateUtils.MILLIS_PER_DAY;
		}
		
		return (System.currentTimeMillis()-time)/danwei;
	}
	
	public static boolean isEmptyList(List<?> list) {
		if (list == null || list.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}
	
	public static void main( String[] args )
	{
		long a = Long.MAX_VALUE;
		System.out.println( Integer.MAX_VALUE > a );
		
		System.out.println( formatDouble( 2.314325,"0.0" ) );
	}
	
	
	/**
	 * 计算列表中shopid数，一个通用计算方法
	 * @param cList
	 * @param shopid
	 * @return
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public static int calShopCount( List<?> cList, String content, String columename ) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		if( ObjectUtils.isEmptyList(cList) )
		{
			return 0;
		}
		else
		{
			int count = 0;
			for( Object obj:cList )
			{
				if( StringUtils.equals(BeanUtils.getProperty(obj, columename), content) )
				{
					count++;
				}
			}
			
			return count;
		}
	}
//
//	
//	
//	/**
//	 * 通用去重
//	 * @param sList
//	 */
//	public static void  filterObjectList(List<?> sList) {
//		if (!ObjectUtils.isEmptyList(sList)) {
//			Iterator<?> ite = sList.iterator();
//
//			Map<Integer, String> fMap = new HashMap<Integer, String>();
//			while (ite.hasNext()) {
//				String 
//				int id = ite.next().getId();
//				if (fMap.get(id) != null) {
//					ite.remove();
//				} else {
//					fMap.put(id, "1");
//				}
//			}
//		}
//
//	}
}
